package com.miyake.dsp.osa;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.TreeSet;

import com.miyake.dsp.ComplexArray;
import com.miyake.dsp.ScilabExecute;

public class RbwFactoryDefault {
	class Pair {
		public Pair(double wavelength, double rbw) {
			super();
			this.wavelength = wavelength;
			this.rbw = rbw;
		}
		double wavelength;
		double rbw;
		public double getWavelength() {
			return wavelength;
		}
		public double getRbw() {
			return rbw;
		}
		
	}
	
	private Map<Double, List<Pair>> rbws = new HashMap<Double, List<Pair>>();
	
	public RbwFactoryDefault(Map<Double, Map<Integer, Double>> aMap) {
		
		setData(aMap);
		
	}

	public void setData(Map<Double, Map<Integer, Double>> aMap) {
		for (Double keyRbw : aMap.keySet()) {
			Map<Integer, Double> m = aMap.get(keyRbw);
			Set<Integer> set = new TreeSet(m.keySet());
			
			List<Pair> list = new ArrayList<Pair>();
			for (Integer keyWav : set) {
				list.add(new Pair(Double.valueOf(keyWav), m.get(keyWav).doubleValue()));
			}
			rbws.put(keyRbw, list);
		}
	}
	
	public RbwFactoryDefault() {
		
		{
			// 30pm
			List<Pair> list = new ArrayList<Pair>();
			list.add(new Pair(600, 0.03));
			list.add(new Pair(1000, 0.029));
			list.add(new Pair(1600, 0.031));
			rbws.put(0.03, list);
		}
		
		{
		// 50pm
			List<Pair> list = new ArrayList<Pair>();
			list.add(new Pair(600, 0.9));
			list.add(new Pair(1000, 0.95));
			list.add(new Pair(1600, 1.15));
			rbws.put(0.05, list);
		}
		{
		// 70pm
			List<Pair> list = new ArrayList<Pair>();
			list.add(new Pair(600, 0.9));
			list.add(new Pair(1000, 0.95));
			list.add(new Pair(1600, 1.15));
			rbws.put(0.07, list);
		}
		
		// 100pm
		{
			List<Pair> list = new ArrayList<Pair>();
			list.add(new Pair(600, 0.11));
			list.add(new Pair(1000, 0.12));
			list.add(new Pair(1600, 0.13));
			rbws.put(0.1, list);
		}
		
		{
		// 200pm
			List<Pair> list = new ArrayList<Pair>();
			list.add(new Pair(600, 0.9));
			list.add(new Pair(1000, 0.95));
			list.add(new Pair(1600, 1.15));
			rbws.put(0.2, list);
		}
		
		{
		// 500pm
			List<Pair> list = new ArrayList<Pair>();
			list.add(new Pair(600, 0.9));
			list.add(new Pair(1000, 0.95));
			list.add(new Pair(1600, 1.15));
			rbws.put(0.5, list);
		}
		
		{
		// 1nm
			List<Pair> list = new ArrayList<Pair>();
			list.add(new Pair(600, 0.9));
			list.add(new Pair(1000, 0.95));
			list.add(new Pair(1600, 1.15));
			rbws.put(1.0, list);
		}
/*        XStream xs = new XStream();

        //Write to a file in the file system
        try {
            FileOutputStream fs = new FileOutputStream("D:\\workspace_indigo\\OtdrSimulator\\rbw.xml");
            xs.toXML(rbws, fs);
        } catch (FileNotFoundException e1) {
            e1.printStackTrace();
        }
*/
	}
	
	
	public double getRBW(Double resolution, double wavelength) {
		List<Pair> list = rbws.get(resolution);
		
		double[] wav = new double[2];
		double[] res = new double[2];
		for (Pair p : list) {
			if (p.getWavelength() >= wavelength) {
				wav[1] = p.getWavelength();
				res[1] = p.getRbw();
				break;
			}
			wav[0] = p.getWavelength();
			res[0] = p.getRbw();
		}

		return ScilabExecute.execute("ret=interpln([data1;data2], " + String.valueOf(OsaTestTool.toVacuum(wavelength))+ ");", new ComplexArray(wav), new ComplexArray(res), "ret").getRealPart()[0];
	}
}
